//
// Created by Asus on 2025/6/2.
//

#include <algorithm>
#include <iostream>
#include <vector>
#include <bits/ostream.tcc>
using namespace std;

void myPrint(int val) {
    cout << val << " ";
}

void demo01() {
    vector<int> v;
    v.push_back(10);
    v.push_back(30);
    v.push_back(50);
    v.push_back(20);
    v.push_back(40);
    sort(v.begin(), v.end());
    for (int i: v) {
        cout << i << " ";
    }
    cout << endl;
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
    for (auto it = v.begin(); it != v.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;

    sort(v.begin(), v.end(), greater<int>());
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
}

class myPrint2
{
public:
    void operator()(int val)
    {
        cout << val << " ";
    }
};

void demo02() {
    srand((unsigned int)time(NULL));
    vector<int> v;
    for(int i = 0 ; i < 10;i++)
    {
        v.push_back(i);
    }
    for_each(v.begin(), v.end(), myPrint2());
    cout << endl;

    //打乱顺序
    random_shuffle(v.begin(), v.end()-4);
    for_each(v.begin(), v.end(), myPrint2());
    cout << endl;
}

class myPrint3
{
public:
    void operator()(int val)
    {
        cout << val << " ";
    }
};

void demo03() {
    vector<int> v1;
    vector<int> v2;
    for (int i = 0; i < 10 ; i++)
    {
        v1.push_back(i);
        v2.push_back(i + 1);
    }

    vector<int> vtarget;
    //目标容器需要提前开辟空间
    vtarget.resize(v1.size() + v2.size());
    //合并  需要两个有序序列
    merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vtarget.begin());
    for_each(vtarget.begin(), vtarget.end(), myPrint3());
    cout << endl;
}

void demo04() {
    vector<int> v;
    v.push_back(10);
    v.push_back(30);
    v.push_back(50);
    v.push_back(20);
    v.push_back(40);

    cout << "反转前： " << endl;
    for_each(v.begin(), v.end(), myPrint2());
    cout << endl;

    cout << "反转后： " << endl;

    reverse(v.begin(), v.end());
    for_each(v.begin(), v.end(), myPrint2());
    cout << endl;
}

int main(int argc, char *argv[]) {
    // demo01();
    // demo02();
    // demo03();
    demo04();
    return 0;
}
